﻿#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QMessageBox>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    tcpsocket = new QTcpSocket(this);
    udpsocket = new QUdpSocket(this);
    QObject::connect(tcpsocket,SIGNAL(connected()),this,SLOT(connect_slot()));
    QObject::connect(tcpsocket,SIGNAL(disconnected()),this,SLOT(disconnect_slot()));
    QObject::connect(tcpsocket,SIGNAL(readyRead()),this,SLOT(readyread_slot()));


    QObject::connect(udpsocket,SIGNAL(readyRead()),this,SLOT(readyreadudp_slot()));

    tcp_timer = new QTimer(this);
    connect(tcp_timer, SIGNAL(timeout()), this, SLOT(tcp_time_slot()));
    connect_timer = new QTimer(this);
    connect(connect_timer, SIGNAL(timeout()), this, SLOT(connect_time_slot()));
    update_timer = new QTimer(this);
    connect(update_timer, SIGNAL(timeout()), this, SLOT(update_time_slot()));
    tcp_time_func = 0;

    _motion_param = new motion_param();

    move_timer = new QTimer(this);
    connect(move_timer, SIGNAL(timeout()), this, SLOT(move_time_slot()));

    //ui->io_in_1->setStyleSheet("QLabel { background-color: blue; }");

    connect_timer->start(1000);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::connect_slot()
{
    //QMessageBox::about(NULL, QString::fromLocal8Bit("状态"), "connect ok");
    qDebug("connect ok\n");
    connect_sta = 1;
    tcp_timer->start(10);
    char buf[32];
    buf[0] = MOTION_GET_BOARD_INFO;
    sendtocom(buf,1);
}

void MainWindow::disconnect_slot()
{
    connect_sta = 0;
    tcp_timer->stop();
    //QMessageBox::about(NULL, QString::fromLocal8Bit("状态"), "disconnect ok");
    qDebug("disconnect ok\n");
}

void MainWindow::readyread_slot()
{
    char rcv_buf[1024];
    char rcv_tcp_buf[1024];
    uint16_t rcv_tcp_len = 0;
    uint16_t rcv_len;
    uint16_t rcv_dat_len;
    rcv_tcp_len = tcpsocket->read(rcv_tcp_buf,1024);
    uint8_t flag = 0;
    for(uint16_t i=0;i<rcv_tcp_len;i++)
    {
        //qDebug("rcv %#x",rcv_tcp_buf[i]);
        switch (flag)
        {
        case 0:
            if(rcv_tcp_buf[i] == (char)0xaa)
            {
                rcv_len = 0;
                rcv_dat_len = 0;
                rcv_buf[rcv_len] = rcv_tcp_buf[i];
                rcv_len++;
                flag = 1;
            }
           break;
        case 1:
        {
            rcv_buf[rcv_len] = rcv_tcp_buf[i];
            rcv_len++;
            if(rcv_len == 3)
            {
                rcv_dat_len = rcv_buf[1] + (rcv_buf[2]<<8);
                flag = 2;
            }
        }
            break;
        case 2:
        {
            rcv_buf[rcv_len] = rcv_tcp_buf[i];
            rcv_len++;
            if(rcv_len == rcv_dat_len + 3)
            {
                flag = 3;
            }

        }
            break;
        case 3:
        {
            motion_req_proc(&rcv_buf[3],rcv_dat_len);
            flag = 0;
        }
            break;
        }

    }
    //qDebug(tcpsocket->readAll());
}

void MainWindow::readyreadudp_slot()
{
    char rcv_buf[1024];
    char rcv_tcp_buf[1024];
    uint16_t rcv_tcp_len = 0;
    uint16_t rcv_len;
    uint16_t rcv_dat_len;
    QHostAddress    peerAddr;
    quint16 peerPort;   //IP地址和端口是可选参数，可用于读取指定主机端口的数据
    rcv_tcp_len = udpsocket->readDatagram(rcv_tcp_buf,1024,&peerAddr,&peerPort);
    uint8_t flag = 0;
    for(uint16_t i=0;i<rcv_tcp_len;i++)
    {
        //qDebug("rcv %#x",rcv_tcp_buf[i]);
        switch (flag)
        {
        case 0:
            if(rcv_tcp_buf[i] == (char)0xaa)
            {
                rcv_len = 0;
                rcv_dat_len = 0;
                rcv_buf[rcv_len] = rcv_tcp_buf[i];
                rcv_len++;
                flag = 1;
            }
           break;
        case 1:
        {
            rcv_buf[rcv_len] = rcv_tcp_buf[i];
            rcv_len++;
            if(rcv_len == 3)
            {
                rcv_dat_len = rcv_buf[1] + (rcv_buf[2]<<8);
                flag = 2;
            }
        }
            break;
        case 2:
        {
            rcv_buf[rcv_len] = rcv_tcp_buf[i];
            rcv_len++;
            if(rcv_len == rcv_dat_len + 3)
            {
                flag = 3;
            }

        }
            break;
        case 3:
        {
            motion_req_proc(&rcv_buf[3],rcv_dat_len);
            flag = 0;
        }
            break;
        }

    }
    //qDebug(tcpsocket->readAll());
}
void MainWindow::motion_req_proc(char *buf,uint32_t len)
{

    switch (buf[0])
    {
        case MOTION_GET_SYS_STATUS:
        {
            switch (buf[1])
            {
                case 0:
                    ui->label_sys_status->setText("READY");
                break;
                case 1:
                    ui->label_sys_status->setText("INIT");
                break;
                case 2:
                    ui->label_sys_status->setText("CLEAN");
                break;
                case 3:
                    ui->label_sys_status->setText("DOWET");
                break;
                case 4:
                    ui->label_sys_status->setText("OUTWET");
                break;
                case 5:
                    ui->label_sys_status->setText("MOVING");
                break;
            }
        }
        break;
        case MOTION_GET_COOR:
        {
            int64_t coor = 0;
            memcpy(&coor,&buf[2],sizeof(int64_t));
            switch (buf[1])
            {
                case AXIS1:
                    ui->coorNumber->display((int)coor);
                    break;
                case AXIS2:
                    ui->coor2Number->display((int)coor);
                    break;
                case AXIS3:
                    ui->coor3Number->display((int)coor);
                    break;
                case AXIS4:
                    ui->coor4Number->display((int)coor);
                    break;
                case AXIS5:
                    ui->coor5Number->display((int)coor);
                    break;
                case AXIS6:
                    ui->coor6Number->display((int)coor);
                    break;
                case AXIS7:
                    ui->coor7Number->display((int)coor);
                    break;
                case AXIS8:
                    ui->coor8Number->display((int)coor);
                    break;

            }
            break;
        }
    case MOTION_IS_MOVING:
    {
        switch ((uint8_t)buf[1])
        {
            case AXIS1:
                axis_ismoving[0] = buf[2];
            break;
            case AXIS2:
                axis_ismoving[1] = buf[2];
            break;
            case AXIS3:
                axis_ismoving[2] = buf[2];
            break;
            case AXIS4:
                axis_ismoving[3] = buf[2];
            break;
            case AXIS5:
                axis_ismoving[4] = buf[2];
            break;
            case AXIS6:
                axis_ismoving[5] = buf[2];
            break;
            case AXIS7:
                axis_ismoving[6] = buf[2];
            break;
            case AXIS8:
                axis_ismoving[7] = buf[2];
            break;
            case 0xff:
                axis_ismoving[0] = buf[2];
                axis_ismoving[1] = buf[3];
                axis_ismoving[2] = buf[4];
                axis_ismoving[3] = buf[5];
                axis_ismoving[4] = buf[6];
                axis_ismoving[5] = buf[7];
                axis_ismoving[6] = buf[8];
                axis_ismoving[7] = buf[9];
            break;
        }
        if(axis_ismoving[0])
        {
            ui->axis1sta->setText("moving");
        }
        else
        {
            ui->axis1sta->setText("stop");
        }
        if(axis_ismoving[1])
        {
            ui->axis2sta->setText("moving");
        }
        else
        {
            ui->axis2sta->setText("stop");
        }
        if(axis_ismoving[2])
        {
            ui->axis3sta->setText("moving");
        }
        else
        {
            ui->axis3sta->setText("stop");
        }
        if(axis_ismoving[3])
        {
            ui->axis4sta->setText("moving");
        }
        else
        {
            ui->axis4sta->setText("stop");
        }
        if(axis_ismoving[4])
        {
            ui->axis5sta->setText("moving");
        }
        else
        {
            ui->axis5sta->setText("stop");
        }
        if(axis_ismoving[5])
        {
            ui->axis6sta->setText("moving");
        }
        else
        {
            ui->axis6sta->setText("stop");
        }
        if(axis_ismoving[6])
        {
            ui->axis7sta->setText("moving");
        }
        else
        {
            ui->axis7sta->setText("stop");
        }
        if(axis_ismoving[7])
        {
            ui->axis8sta->setText("moving");
        }
        else
        {
            ui->axis8sta->setText("stop");
        }


        break;
    }
    case MOTION_UPDATE_START:
    case MOTION_UPDATE_DATA:
    case MOTION_UPDATE_END:
    update_rcv_flag = 1;
        break;

    case MOTION_IO_GET_IN:
    {
        for(uint8_t i=0;i<16;i++)
        {
            switch (i)
            {
                case 0:
                    if(buf[1+i])
                    {
                        ui->io_in_1->setStyleSheet("QLabel { background-color: blue; }");
                    }
                    else
                    {
                        ui->io_in_1->setStyleSheet("QLabel { background-color: green; }");
                    }
                break;
                case 1:
                    if(buf[1+i])
                    {
                        ui->io_in_2->setStyleSheet("QLabel { background-color: blue; }");
                    }
                    else
                    {
                        ui->io_in_2->setStyleSheet("QLabel { background-color: green; }");
                    }
                break;
                case 2:
                    if(buf[1+i])
                    {
                        ui->io_in_3->setStyleSheet("QLabel { background-color: blue; }");
                    }
                    else
                    {
                        ui->io_in_3->setStyleSheet("QLabel { background-color: green; }");
                    }
                break;
                case 3:
                    if(buf[1+i])
                    {
                        ui->io_in_4->setStyleSheet("QLabel { background-color: blue; }");
                    }
                    else
                    {
                        ui->io_in_4->setStyleSheet("QLabel { background-color: green; }");
                    }
                break;
                case 4:
                    if(buf[1+i])
                    {
                        ui->io_in_5->setStyleSheet("QLabel { background-color: blue; }");
                    }
                    else
                    {
                        ui->io_in_5->setStyleSheet("QLabel { background-color: green; }");
                    }
                break;
                case 5:
                    if(buf[1+i])
                    {
                        ui->io_in_6->setStyleSheet("QLabel { background-color: blue; }");
                    }
                    else
                    {
                        ui->io_in_6->setStyleSheet("QLabel { background-color: green; }");
                    }
                break;
                case 6:
                    if(buf[1+i])
                    {
                        ui->io_in_7->setStyleSheet("QLabel { background-color: blue; }");
                    }
                    else
                    {
                        ui->io_in_7->setStyleSheet("QLabel { background-color: green; }");
                    }
                break;
                case 7:
                    if(buf[1+i])
                    {
                        ui->io_in_8->setStyleSheet("QLabel { background-color: blue; }");
                    }
                    else
                    {
                        ui->io_in_8->setStyleSheet("QLabel { background-color: green; }");
                    }
                break;
                case 8:
                    if(buf[1+i])
                    {
                        ui->io_in_9->setStyleSheet("QLabel { background-color: blue; }");
                    }
                    else
                    {
                        ui->io_in_9->setStyleSheet("QLabel { background-color: green; }");
                    }
                break;
                case 9:
                    if(buf[1+i])
                    {
                        ui->io_in_10->setStyleSheet("QLabel { background-color: blue; }");
                    }
                    else
                    {
                        ui->io_in_10->setStyleSheet("QLabel { background-color: green; }");
                    }
                break;
                case 10:
                    if(buf[1+i])
                    {
                        ui->io_in_11->setStyleSheet("QLabel { background-color: blue; }");
                    }
                    else
                    {
                        ui->io_in_11->setStyleSheet("QLabel { background-color: green; }");
                    }
                break;
                case 11:
                    if(buf[1+i])
                    {
                        ui->io_in_12->setStyleSheet("QLabel { background-color: blue; }");
                    }
                    else
                    {
                        ui->io_in_12->setStyleSheet("QLabel { background-color: green; }");
                    }
                break;
                case 12:
                    if(buf[1+i])
                    {
                        ui->io_in_13->setStyleSheet("QLabel { background-color: blue; }");
                    }
                    else
                    {
                        ui->io_in_13->setStyleSheet("QLabel { background-color: green; }");
                    }
                break;
                case 13:
                    if(buf[1+i])
                    {
                        ui->io_in_14->setStyleSheet("QLabel { background-color: blue; }");
                    }
                    else
                    {
                        ui->io_in_14->setStyleSheet("QLabel { background-color: green; }");
                    }
                break;
                case 14:
                    if(buf[1+i])
                    {
                        ui->io_in_15->setStyleSheet("QLabel { background-color: blue; }");
                    }
                    else
                    {
                        ui->io_in_15->setStyleSheet("QLabel { background-color: green; }");
                    }
                break;
                case 15:
                    if(buf[1+i])
                    {
                        ui->io_in_16->setStyleSheet("QLabel { background-color: blue; }");
                    }
                    else
                    {
                        ui->io_in_16->setStyleSheet("QLabel { background-color: green; }");
                    }
                break;
                }
            }
        }
        break;
    case MOTION_IO_GET_OUT:
    {
        for(uint8_t i=0;i<16;i++)
        {
            switch (i)
            {
                case 0:
                    if(buf[1+i])
                    {
                        ui->io_out_1->setStyleSheet("QLabel { background-color: blue; }");
                    }
                    else
                    {
                        ui->io_out_1->setStyleSheet("QLabel { background-color: green; }");
                    }
                break;
                case 1:
                    if(buf[1+i])
                    {
                        ui->io_out_2->setStyleSheet("QLabel { background-color: blue; }");
                    }
                    else
                    {
                        ui->io_out_2->setStyleSheet("QLabel { background-color: green; }");
                    }
                break;
                case 2:
                    if(buf[1+i])
                    {
                        ui->io_out_3->setStyleSheet("QLabel { background-color: blue; }");
                    }
                    else
                    {
                        ui->io_out_3->setStyleSheet("QLabel { background-color: green; }");
                    }
                break;
                case 3:
                    if(buf[1+i])
                    {
                        ui->io_out_4->setStyleSheet("QLabel { background-color: blue; }");
                    }
                    else
                    {
                        ui->io_out_4->setStyleSheet("QLabel { background-color: green; }");
                    }
                break;
                case 4:
                    if(buf[1+i])
                    {
                        ui->io_out_5->setStyleSheet("QLabel { background-color: blue; }");
                    }
                    else
                    {
                        ui->io_out_5->setStyleSheet("QLabel { background-color: green; }");
                    }
                break;
                case 5:
                    if(buf[1+i])
                    {
                        ui->io_out_6->setStyleSheet("QLabel { background-color: blue; }");
                    }
                    else
                    {
                        ui->io_out_6->setStyleSheet("QLabel { background-color: green; }");
                    }
                break;
                case 6:
                    if(buf[1+i])
                    {
                        ui->io_out_7->setStyleSheet("QLabel { background-color: blue; }");
                    }
                    else
                    {
                        ui->io_out_7->setStyleSheet("QLabel { background-color: green; }");
                    }
                break;
                case 7:
                    if(buf[1+i])
                    {
                        ui->io_out_8->setStyleSheet("QLabel { background-color: blue; }");
                    }
                    else
                    {
                        ui->io_out_8->setStyleSheet("QLabel { background-color: green; }");
                    }
                break;
                case 8:
                    if(buf[1+i])
                    {
                        ui->io_out_9->setStyleSheet("QLabel { background-color: blue; }");
                    }
                    else
                    {
                        ui->io_out_9->setStyleSheet("QLabel { background-color: green; }");
                    }
                break;
                case 9:
                    if(buf[1+i])
                    {
                        ui->io_out_10->setStyleSheet("QLabel { background-color: blue; }");
                    }
                    else
                    {
                        ui->io_out_10->setStyleSheet("QLabel { background-color: green; }");
                    }
                break;
                case 10:
                    if(buf[1+i])
                    {
                        ui->io_out_11->setStyleSheet("QLabel { background-color: blue; }");
                    }
                    else
                    {
                        ui->io_out_11->setStyleSheet("QLabel { background-color: green; }");
                    }
                break;
                case 11:
                    if(buf[1+i])
                    {
                        ui->io_out_12->setStyleSheet("QLabel { background-color: blue; }");
                    }
                    else
                    {
                        ui->io_out_12->setStyleSheet("QLabel { background-color: green; }");
                    }
                break;
                case 12:
                    if(buf[1+i])
                    {
                        ui->io_out_13->setStyleSheet("QLabel { background-color: blue; }");
                    }
                    else
                    {
                        ui->io_out_13->setStyleSheet("QLabel { background-color: green; }");
                    }
                break;
                case 13:
                    if(buf[1+i])
                    {
                        ui->io_out_14->setStyleSheet("QLabel { background-color: blue; }");
                    }
                    else
                    {
                        ui->io_out_14->setStyleSheet("QLabel { background-color: green; }");
                    }
                break;
                case 14:
                    if(buf[1+i])
                    {
                        ui->io_out_15->setStyleSheet("QLabel { background-color: blue; }");
                    }
                    else
                    {
                        ui->io_out_15->setStyleSheet("QLabel { background-color: green; }");
                    }
                break;
                case 15:
                    if(buf[1+i])
                    {
                        ui->io_out_16->setStyleSheet("QLabel { background-color: blue; }");
                    }
                    else
                    {
                        ui->io_out_16->setStyleSheet("QLabel { background-color: green; }");
                    }
                break;
                }
            }
        }
        break;
        case MOTION_AXIS_MOVE_FINISH_INFO:
        {
            uint8_t axis = buf[1];
            int64_t coor = 0;
            memcpy(&coor,&buf[2],sizeof(int64_t));
            uint8_t rsp = buf[10];
            qDebug("axis %d rsp %d coor %lld \n",axis,rsp,coor);
        }break;
        case MOTION_GET_MOVEINFO:
        {
            int64_t accnum = 0;
            memcpy(&accnum,&buf[1],sizeof(uint64_t));
            float acc_t ;
            memcpy(&acc_t,&buf[9],sizeof(float));
            int64_t uinnum = 0;
            memcpy(&uinnum,&buf[13],sizeof(uint64_t));
            float uin_t ;
            memcpy(&uin_t,&buf[21],sizeof(float));
            int64_t decnum = 0;
            memcpy(&decnum,&buf[25],sizeof(uint64_t));
            float dec_t ;
            memcpy(&dec_t,&buf[33],sizeof(float));
//            qDebug("accnum %lld acc_t %f",accnum,acc_t);
//            qDebug("acc_t %f",acc_t);
//            qDebug("uinnum %lld uin_t %f",uinnum,uin_t);
//            qDebug("decnum %lld dec_t %f",decnum,dec_t);
//            qDebug("acc_t %x %x %x %x",buf[9],buf[10],buf[11],buf[12]);
//            qDebug("uin_t %x %x %x %x",buf[21],buf[22],buf[23],buf[24]);
//            qDebug("dec_t %x %x %x %x",buf[33],buf[34],buf[35],buf[36]);
            ui->lineEdit_accnum->setText(QString::number(accnum));
            ui->lineEdit_acct->setText(QString::number(acc_t));
            ui->lineEdit_uinnum->setText(QString::number(uinnum));
            ui->lineEdit_uint->setText(QString::number(uin_t));
            ui->lineEdit_decnum->setText(QString::number(decnum));
            ui->lineEdit_dect->setText(QString::number(dec_t));
        }break;
        case MOTION_GET_BOARD_INFO:
        {
            QString str;
            str.sprintf("%s",&buf[1]);
            qDebug()<<str;
            ui->version->setText(str);
        }
        break;
        case MOTION_GET_NET_INFO:
        {
            if(!connect_sta)
            {
                QString str;
                str.sprintf("%d.%d.%d.%d",(uchar)buf[1],(uchar)buf[2],(uchar)buf[3],(uchar)buf[4]);
                uint16_t port = (uchar)buf[5] + (uchar)buf[6] * 256;
                tcpsocket->connectToHost(str,port);
            }
        }
        break;
    }
}

void MainWindow::sendtocom(char *buf,uint32_t len)
{
    char buff[1024];
    buff[0] = 0xaa;
    buff[1] = len;
    buff[2] = len>>8;
    memcpy(&buff[3],buf,len);
    buff[len+3] = 0x55;
    tcpsocket->write(buff,len+4);
}

void MainWindow::sendtoudpcom(char *buf,uint32_t len)
{
    char buff[1024];
    buff[0] = 0xaa;
    buff[1] = len;
    buff[2] = len>>8;
    memcpy(&buff[3],buf,len);
    buff[len+3] = 0x55;
    qint64 sendlen = len+4;
    QHostAddress  targetAddr("192.168.101.255");
    udpsocket->writeDatagram((const char*)buff,sendlen, targetAddr, 5000);
}
void MainWindow::update_time_slot()
{
        switch (update_send_flag)
        {
        case 100:
            {
                char buf[32];
                buf[0] = MOTION_UPDATE_START;
                buf[1] = 1;
                sendtocom(buf,2);
                update_send_flag = 1;
            }
            break;
        case 101:
            {
                char buf[32];
                buf[0] = MOTION_UPDATE_START;
                buf[1] = 2;
                sendtocom(buf,2);
                update_send_flag = 1;
            }
            break;
        case 1:
        {

            if(update_rcv_flag)
            {
                char file_data[1100];
                int len = file->read(&file_data[1],512);
                if(len == 0)
                {
                    update_send_flag = 2;
                }
                else
                {
                    file_data[0] = MOTION_UPDATE_DATA;
                    sendtocom(file_data,len+1);
                    update_rcv_flag = 0;
                    ui->progressBar->setValue(file->pos()*100/file->size());
                    qDebug("len %d size %lld pos %lld",len,file->size(),file->pos());
                }
            }
        }
            break;
        case 2:
        {

            if(update_rcv_flag)
            {
                file->close();
                char buf[32];
                buf[0] = MOTION_UPDATE_END;
                sendtocom(buf,1);
                update_rcv_flag = 0;
                update_send_flag = 3;
            }
        }
            break;
        case 3:
        {
            QMessageBox::about(NULL, QString::fromLocal8Bit("升级完成"), "ok");
            update_timer->stop();
        }
            break;
        }
}

void MainWindow::connect_time_slot()
{
    if(connect_sta)
    {
        ui->ConnectButton->setStyleSheet("QPushButton { background-color: green; color: white; }");
        if(connect_bt == 0)
        {
            tcpsocket->close();
        }
    }
    else
    {
        ui->ConnectButton->setStyleSheet("QPushButton { background-color: red; color: white; }");
        if(connect_bt == 1)
        {
            char buf[32];
            buf[0] = MOTION_GET_NET_INFO;
            sendtoudpcom(buf,1);
        }
    }
}

void MainWindow::tcp_time_slot()
{
    switch (tcp_time_func)
    {
    case 0:
    {
        char buf[32];
        buf[0] = MOTION_GET_COOR;
        buf[1] = AXIS1;
        sendtocom(buf,2);
    }
        break;
    case 1:
    {
        char buf[32];
        buf[0] = MOTION_GET_COOR;
        buf[1] = AXIS2;
        sendtocom(buf,2);
    }
        break;
    case 2:
    {
        char buf[32];
        buf[0] = MOTION_GET_COOR;
        buf[1] = AXIS3;
        sendtocom(buf,2);
    }
        break;
    case 3:
    {
        char buf[32];
        buf[0] = MOTION_GET_COOR;
        buf[1] = AXIS4;
        sendtocom(buf,2);
    }
        break;
    case 4:
    {
        char buf[32];
        buf[0] = MOTION_IS_MOVING;
        buf[1] = 0xff;
        sendtocom(buf,2);
    }
        break;
    case 5:
    {
        char buf[32];
        buf[0] = MOTION_IO_GET_IN;
        sendtocom(buf,1);
    }
        break;
    case 6:
    {
        char buf[32];
        buf[0] = MOTION_IO_GET_OUT;
        sendtocom(buf,1);
    }
        break;
    case 7:
    {
        char buf[32];
        buf[0] = MOTION_GET_SYS_STATUS;
        sendtocom(buf,1);
    }
        break;
    case 8:
    {
        char buf[32];
        buf[0] = MOTION_GET_COOR;
        buf[1] = AXIS5;
        sendtocom(buf,2);
    }
        break;
    case 9:
    {
        char buf[32];
        buf[0] = MOTION_GET_COOR;
        buf[1] = AXIS6;
        sendtocom(buf,2);
    }
        break;
    case 10:
    {
        char buf[32];
        buf[0] = MOTION_GET_COOR;
        buf[1] = AXIS7;
        sendtocom(buf,2);
    }
        break;
    case 11:
    {
        char buf[32];
        buf[0] = MOTION_GET_COOR;
        buf[1] = AXIS8;
        sendtocom(buf,2);
    }
        break;

    }
    tcp_time_func++;
    if(tcp_time_func > 11)
    {
        tcp_time_func = 0;
    }
}

void MainWindow::on_ForwordButton_clicked()
{
    move_time_func = 0;
    move_timer->start(10);
}

void MainWindow::on_ConnectButton_clicked()
{
    //tcpsocket->connectToHost("192.168.101.5",1234);

    connect_bt = 1;
}

void MainWindow::on_stopButton_clicked()
{
    char buf[32];
    buf[0] = MOTION_STOP;
    buf[1] = 0xff;
    sendtocom(buf,2);
}

void MainWindow::on_BackButton_clicked()
{
    move_timer->stop();
}

void MainWindow::on_pushFButton_pressed()
{
    char buf[32];
    buf[0] = MOTION_MOVE_LEN;
    buf[1] = ui->axiscomboBox_2->currentIndex();
    int64_t dest;
    dest = 10000000;
    memcpy(&buf[2],&dest,sizeof(int64_t));
    uint32_t speed = ui->diandong1->text().toInt();
    memcpy(&buf[10],&speed,sizeof(uint32_t));
    sendtocom(buf,15);

}

void MainWindow::on_pushFButton_released()
{
    char buf[32];
    buf[0] = MOTION_STOP;
    buf[1] = 0xff;
    buf[2] = 0;
    sendtocom(buf,3);

}

void MainWindow::on_pushBButton_pressed()
{
    char buf[32];
    buf[0] = MOTION_MOVE_LEN;
    buf[1] = ui->axiscomboBox_2->currentIndex();
    int64_t dest;
    dest = -100000000;
    memcpy(&buf[2],&dest,sizeof(int64_t));
    uint32_t speed = ui->diandong1->text().toInt();
    memcpy(&buf[10],&speed,sizeof(uint32_t));
    sendtocom(buf,15);

}

void MainWindow::on_pushBButton_released()
{
    char buf[32];
    buf[0] = MOTION_STOP;
    buf[1] = 0xff;
    buf[2] = 0;
    sendtocom(buf,3);

}

void MainWindow::on_closeButton_clicked()
{
    connect_bt = 0;
}

void MainWindow::on_movetor1Button_clicked()
{
    int64_t dest = ui->destEdit->text().toLongLong();
    uint32_t speed = ui->speedEdit->text().toInt();
    char buf[32];
    buf[0] = MOTION_MOVE_TO;
    buf[1] = ui->axiscomboBox->currentIndex();
    memcpy(&buf[2],&dest,sizeof(int64_t));
    memcpy(&buf[10],&speed,sizeof(uint32_t));
    sendtocom(buf,15);
}

void MainWindow::on_updateButton_clicked()
{
    QString directory = QDir::toNativeSeparators(QFileDialog::getOpenFileName(this, QString::fromLocal8Bit("标题"), ".", "*.dat"));
    if (!directory.isEmpty())
    {
        file = new QFile(directory);
        //file(directory);
        if(!file->open(QIODevice::ReadOnly))
        {
            qDebug("Can't open the file!\r\n");
            return ;
        }
        update_send_flag = 100;
        update_rcv_flag = 0;
        update_timer->start(10);
    }
}

void MainWindow::on_movelenr1Button_clicked()
{
    int64_t dest = ui->destEdit_2->text().toLongLong();
    uint32_t speed = ui->speedEdit_2->text().toInt();
    char buf[32];
    buf[0] = MOTION_MOVE_LEN;
    buf[1] = ui->axiscomboBox_3->currentIndex();
    memcpy(&buf[2],&dest,sizeof(int64_t));
    memcpy(&buf[10],&speed,sizeof(uint32_t));
    sendtocom(buf,15);
}

//void MainWindow::on_movetor2Button_clicked()
//{
//    int64_t dest = ui->dest2Edit->text().toLongLong();
//    uint32_t speed = ui->speed2Edit->text().toInt();
//    char buf[32];
//    buf[0] = 8;
//    buf[1] = AXIS2;
//    memcpy(&buf[2],&dest,sizeof(int64_t));
//    memcpy(&buf[10],&speed,sizeof(uint32_t));
//    sendtocom(buf,15);
//}

//void MainWindow::on_movelenr2Button_clicked()
//{
//    int64_t dest = ui->dest4Edit->text().toLongLong();
//    uint32_t speed = ui->speed4Edit->text().toInt();
//    char buf[32];
//    buf[0] = MOTION_MOVE_LEN;
//    buf[1] = AXIS2;
//    memcpy(&buf[2],&dest,sizeof(int64_t));
//    memcpy(&buf[10],&speed,sizeof(uint32_t));
//    sendtocom(buf,15);
//}

void MainWindow::on_motionButton_clicked()
{
    QString directory = QDir::toNativeSeparators(QFileDialog::getOpenFileName(this, QString::fromLocal8Bit("标题"), ".", "*.json"));
    if (!directory.isEmpty())
    {
        file = new QFile(directory);
        //file(directory);
        if(!file->open(QIODevice::ReadOnly))
        {
            qDebug("Can't open the file!\r\n");
            return ;
        }
        update_send_flag = 101;
        update_rcv_flag = 0;
        update_timer->start(10);
    }
}

void MainWindow::on_movelenr1Buttonex_clicked()
{
    float dest = ui->destEdit_3->text().toFloat();
    float speed = ui->speedEdit_3->text().toFloat();
    char buf[32];
    buf[0] = MOTION_MOVE_LENEX;
    buf[1] = ui->axiscomboBox_4->currentIndex();
    memcpy(&buf[2],&dest,sizeof(float));
    memcpy(&buf[6],&speed,sizeof(float));
    sendtocom(buf,10);
}

void MainWindow::move_time_slot()
{
    switch (move_time_func)
    {
    case 0:
    {
        float dest = ui->destEdit_3->text().toFloat();
        float speed = ui->speedEdit_3->text().toFloat();
        char buf[32];
        buf[0] = MOTION_MOVE_LENEX;
        buf[1] = ui->axiscomboBox_4->currentIndex();
        memcpy(&buf[2],&dest,sizeof(float));
        memcpy(&buf[6],&speed,sizeof(float));
        sendtocom(buf,10);
        move_time_func = 1;
        break;
    }
    case 1:
    {
        if(axis_ismoving[0] == 1)
        {
            move_time_func = 2;
        }
        break;
    }
    case 2:
    {
        if(axis_ismoving[0] == 0)
        {
            move_time_func = 3;
        }
        break;
    }
    case 3:
    {
        float dest = -ui->destEdit_3->text().toFloat();
        float speed = ui->speedEdit_3->text().toFloat();
        char buf[32];
        buf[0] = MOTION_MOVE_LENEX;
        buf[1] = ui->axiscomboBox_4->currentIndex();
        memcpy(&buf[2],&dest,sizeof(float));
        memcpy(&buf[6],&speed,sizeof(float));
        sendtocom(buf,10);
        move_time_func = 4;
        break;
    }
    case 4:
    {
        if(axis_ismoving[0] == 1)
        {
            move_time_func = 5;
        }
        break;
    }
    case 5:
    {
        if(axis_ismoving[0] == 0)
        {
            move_time_func = 0;
        }
        break;
    }

    }
}

//void MainWindow::on_push3FButton_pressed()
//{
//    char buf[32];
//    buf[0] = MOTION_MOVE_LEN;
//    buf[1] = AXIS3;
//    int64_t dest;
//    dest = 10000000;
//    memcpy(&buf[2],&dest,sizeof(int64_t));
//    uint32_t speed = ui->diandong3->text().toInt();
//    memcpy(&buf[10],&speed,sizeof(uint32_t));
//    sendtocom(buf,15);
//}

//void MainWindow::on_push3FButton_released()
//{
//    char buf[32];
//    buf[0] = MOTION_STOP;
//    buf[1] = 0xff;
//    buf[2] = 0;
//    sendtocom(buf,3);
//}

//void MainWindow::on_push3BButton_pressed()
//{
//    char buf[32];
//    buf[0] = MOTION_MOVE_LEN;
//    buf[1] = AXIS3;
//    int64_t dest;
//    dest = -10000000;
//    memcpy(&buf[2],&dest,sizeof(int64_t));
//    uint32_t speed = ui->diandong3->text().toInt();
//    memcpy(&buf[10],&speed,sizeof(uint32_t));
//    sendtocom(buf,15);

//}

//void MainWindow::on_push3BButton_released()
//{
//    char buf[32];
//    buf[0] = MOTION_STOP;
//    buf[1] = 0xff;
//    buf[2] = 0;
//    sendtocom(buf,3);

//}

//void MainWindow::on_push4FButton_pressed()
//{
//    char buf[32];
//    buf[0] = MOTION_MOVE_LEN;
//    buf[1] = AXIS4;
//    int64_t dest;
//    dest = 10000000;
//    memcpy(&buf[2],&dest,sizeof(int64_t));
//    uint32_t speed = ui->diandong4->text().toInt();
//    memcpy(&buf[10],&speed,sizeof(uint32_t));
//    sendtocom(buf,15);

//}

//void MainWindow::on_push4FButton_released()
//{
//    char buf[32];
//    buf[0] = MOTION_STOP;
//    buf[1] = 0xff;
//    buf[2] = 0;
//    sendtocom(buf,3);

//}

//void MainWindow::on_push4BButton_pressed()
//{
//    char buf[32];
//    buf[0] = MOTION_MOVE_LEN;
//    buf[1] = AXIS4;
//    int64_t dest;
//    dest = -10000000;
//    memcpy(&buf[2],&dest,sizeof(int64_t));
//    uint32_t speed = ui->diandong4->text().toInt();
//    memcpy(&buf[10],&speed,sizeof(uint32_t));
//    sendtocom(buf,15);

//}

//void MainWindow::on_push4BButton_released()
//{
//    char buf[32];
//    buf[0] = MOTION_STOP;
//    buf[1] = 0xff;
//    buf[2] = 0;
//    sendtocom(buf,3);

//}

void MainWindow::on_pushButton_H_clicked()
{
    char buf[32];
    buf[0] = MOTION_IO_SET_OUT;
    buf[1] = ui->comboBox->currentIndex()+1;
    buf[2] = 1;
    sendtocom(buf,3);
}

void MainWindow::on_pushButton_L_clicked()
{
    char buf[32];
    buf[0] = MOTION_IO_SET_OUT;
    buf[1] = ui->comboBox->currentIndex()+1;
    buf[2] = 0;
    sendtocom(buf,3);
}

//void MainWindow::on_movetor3Button_clicked()
//{
//    int64_t dest = ui->dest3Edit_2->text().toLongLong();
//    uint32_t speed = ui->speed3Edit_2->text().toInt();
//    char buf[32];
//    buf[0] = 8;
//    buf[1] = AXIS3;
//    memcpy(&buf[2],&dest,sizeof(int64_t));
//    memcpy(&buf[10],&speed,sizeof(uint32_t));
//    sendtocom(buf,15);
//}

//void MainWindow::on_movetor4Button_clicked()
//{
//    int64_t dest = ui->dest4Edit_2->text().toLongLong();
//    uint32_t speed = ui->speed4Edit_2->text().toInt();
//    char buf[32];
//    buf[0] = 8;
//    buf[1] = AXIS4;
//    memcpy(&buf[2],&dest,sizeof(int64_t));
//    memcpy(&buf[10],&speed,sizeof(uint32_t));
//    sendtocom(buf,15);
//}

//void MainWindow::on_movelenr1Button_2_clicked()
//{
//    int64_t dest = ui->dest7Edit->text().toLongLong();
//    uint32_t speed = ui->speed7Edit->text().toInt();
//    char buf[32];
//    buf[0] = MOTION_MOVE_LEN;
//    buf[1] = AXIS3;
//    memcpy(&buf[2],&dest,sizeof(int64_t));
//    memcpy(&buf[10],&speed,sizeof(uint32_t));
//    sendtocom(buf,15);
//}

//void MainWindow::on_movelenr1Button_4_clicked()
//{
//    int64_t dest = ui->dest6Edit->text().toLongLong();
//    uint32_t speed = ui->speed6Edit->text().toInt();
//    char buf[32];
//    buf[0] = MOTION_MOVE_LEN;
//    buf[1] = AXIS4;
//    memcpy(&buf[2],&dest,sizeof(int64_t));
//    memcpy(&buf[10],&speed,sizeof(uint32_t));
//    sendtocom(buf,15);

//}

//void MainWindow::on_movelenr2Buttonex_clicked()
//{
//    float dest = ui->dest10Edit->text().toFloat();
//    float speed = ui->speed10Edit->text().toFloat();
//    char buf[32];
//    buf[0] = MOTION_MOVE_LENEX;
//    buf[1] = AXIS2;
//    memcpy(&buf[2],&dest,sizeof(float));
//    memcpy(&buf[6],&speed,sizeof(float));
//    sendtocom(buf,10);
//}

//void MainWindow::on_movelenr3Buttonex_clicked()
//{
//    float dest = ui->dest10Edit->text().toFloat();
//    float speed = ui->speed10Edit->text().toFloat();
//    char buf[32];
//    buf[0] = MOTION_MOVE_LENEX;
//    buf[1] = AXIS3;
//    memcpy(&buf[2],&dest,sizeof(float));
//    memcpy(&buf[6],&speed,sizeof(float));
//    sendtocom(buf,10);

//}

//void MainWindow::on_movelenr4Buttonex_clicked()
//{
//    float dest = ui->dest11Edit->text().toFloat();
//    float speed = ui->speed11Edit->text().toFloat();
//    char buf[32];
//    buf[0] = MOTION_MOVE_LENEX;
//    buf[1] = AXIS4;
//    memcpy(&buf[2],&dest,sizeof(float));
//    memcpy(&buf[6],&speed,sizeof(float));
//    sendtocom(buf,10);

//}

void MainWindow::on_pushBButton_clicked()
{
    char buf[32];
    buf[0] = MOTION_MOVE_LEN;
    buf[1] = ui->axiscomboBox_2->currentIndex();
    int64_t dest;
    dest = -10000000;
    memcpy(&buf[2],&dest,sizeof(int64_t));
    uint32_t speed = ui->diandong1->text().toInt();
    memcpy(&buf[10],&speed,sizeof(uint32_t));
    sendtocom(buf,15);
}

void MainWindow::on_pushFButton_clicked()
{

    char buf[32];
    buf[0] = MOTION_MOVE_LEN;
    buf[1] = ui->axiscomboBox_2->currentIndex();
    int64_t dest;
    dest = 10000000;
    memcpy(&buf[2],&dest,sizeof(int64_t));
    uint32_t speed = ui->diandong1->text().toInt();
    memcpy(&buf[10],&speed,sizeof(uint32_t));
    sendtocom(buf,15);
}

void MainWindow::on_pushButtoncunt_clicked()
{
    char buf[32];
    buf[0] = MOTION_GET_MOVEINFO;
    buf[1] = ui->axiscomboBox_cunt->currentIndex();
    int64_t dest;
    dest = ui->lineEdit_countdest->text().toInt();
    uint32_t speed = ui->lineEdit_countspeed->text().toInt();
    memcpy(&buf[2],&speed,sizeof(uint32_t));
    memcpy(&buf[6],&dest,sizeof(int64_t));
    sendtocom(buf,14);
}

void MainWindow::on_pushButton_clicked()
{
//    char buf[32];
//    buf[0] = MOTION_VENDER_CMD;
//    buf[1] = 5;
//    uint32_t speed = 10000;
//    int64_t dest = 10000;
//    uint8_t axis = 1;
//    memcpy(&buf[2],&axis,sizeof(uint8_t));
//    memcpy(&buf[3],&speed,sizeof(uint32_t));
//    memcpy(&buf[7],&dest,sizeof(int64_t));
//    speed = 10000;
//    dest = 10000;
//    axis = 2;
//    memcpy(&buf[15],&axis,sizeof(uint8_t));
//    memcpy(&buf[16],&speed,sizeof(uint32_t));
//    memcpy(&buf[20],&dest,sizeof(int64_t));
//    sendtocom(buf,29);


        char buf[32];
        buf[0] = MOTION_SET_COOR;
        buf[1] = 0;
        int64_t dest = 10000;
        memcpy(&buf[2],&dest,sizeof(int64_t));
        sendtocom(buf,10);
}
